//
//  HJNetWorkTool.swift
//  sinaWeibo
//
//  Created by hjun on 16/3/22.
//  Copyright © 2016年 hjun. All rights reserved.
//

import UIKit
import AFNetworking
class HJNetWorkTool: NSObject {
    ///单例
    static let sharedInstance: HJNetWorkTool = HJNetWorkTool()
    
    let afnMessage: AFHTTPSessionManager = AFHTTPSessionManager(baseURL: HJBaseUrl)
    
    
    ///私有化构造函数
    
    private override init() {
        super.init()
        ///设置AFN支持 text/plain类型
        afnMessage.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
    }
    
    ///自己封装AFN的GET请求
    
    
    //      HJNetWorkTool.sharedInstance.afnMessage.GET(<#T##URLString: String##String#>, parameters: <#T##AnyObject?#>, progress: <#T##((NSProgress) -> Void)?##((NSProgress) -> Void)?##(NSProgress) -> Void#>, success: <#T##((NSURLSessionDataTask, AnyObject?) -> Void)?##((NSURLSessionDataTask, AnyObject?) -> Void)?##(NSURLSessionDataTask, AnyObject?) -> Void#>, failure: <#T##((NSURLSessionDataTask?, NSError) -> Void)?##((NSURLSessionDataTask?, NSError) -> Void)?##(NSURLSessionDataTask?, NSError) -> Void#>)
    
    func requestGET(URLString: String, parameters: AnyObject?,requestCallBack:(result:[String: AnyObject]?,error: NSError?) ->()){
        
        afnMessage.GET(URLString, parameters: parameters, progress: nil, success: { (_, result) -> Void in
            // 网络请求回来的都是JSON格式
            let dict = result as? [String: AnyObject]
            requestCallBack(result: dict, error: nil)
            }) { (_, error) -> Void in
                requestCallBack(result: nil, error: error)
        }
        
    }
    ///封装POST请求
    
    func requestPOST(URLString: String, parameters: AnyObject?,requestCallBack:(result:[String: AnyObject]?,error: NSError?) ->()){
        
        afnMessage.POST(URLString, parameters: parameters, progress: nil, success: { (_, result) -> Void in
            // 网络请求回来的都是JSON格式
            let dict = result as? [String: AnyObject]
            requestCallBack(result: dict, error: nil)
            
            })
            { (_, error) -> Void in
                
                requestCallBack(result: nil, error: error)
        }
        
        
    }
    
    
}
